#include<semaphore.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#define MAX 256

char* buffer;
sem_t empty;
sem_t full;
sem_t mutex;

void* producer()
{
    sem_wait(&empty);
    sem_wait(&mutex);
    printf("请输入buffer:");

    buffer =(char*)malloc(MAX);
    fgets(buffer,MAX,stdin);
    sem_post(&mutex);
    sem_post(&full);

    return NULL;
}


void* consumer()
{
    sem_wait(&full);
    sem_wait(&mutex);

    

    memset(buffer,0,MAX);
    sem_post(&mutex);
    sem_post(&empty);

    return NULL;
}

int main()
{
    pthread_t id_producer;
    pthread_t id_consumer;

    //int ret;
    sem_init(&empty,0,10);
    sem_init(&full,0,0);
    sem_init(&mutex,0,1);
    pthread_create(&id_producer,NULL,producer,NULL);
    pthread_create(&id_consumer,NULL,consumer,NULL);
    pthread_join(id_producer,NULL);
    pthread_join(id_consumer,NULL);
    sem_destroy(&empty);
    sem_destroy(&full);
    sem_destroy(&mutex);

    printf("程序结束\n");
    return 0;
}
